package com.hontech.bread.ui.parts

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bread.R
import com.hontech.bread.device.mdb.Pos
import com.hontech.bread.device.mdb.PosModelInfo
import com.hontech.bread.device.mdb.PosSetupInfo
import com.hontech.bread.device.mdb.country
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.stringById

class PosInfoParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val ltLevel: LabelTextView
    private val ltCountry: LabelTextView
    private val ltScaling: LabelTextView
    private val ltDecimal: LabelTextView
    private val ltMan: LabelTextView
    private val ltSerial: LabelTextView
    private val ltModel: LabelTextView
    private val ltVersion: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_pos_info, this)
        val btnInit = view.findViewById<Button>(R.id.id_pos_info_btn_init)
        btnInit.onClick { onClickInit() }

        ltLevel = view.findViewById(R.id.id_pos_info_lt_level)
        ltCountry = view.findViewById(R.id.id_pos_info_lt_country_code)
        ltScaling = view.findViewById(R.id.id_pos_info_lt_scaling)
        ltDecimal = view.findViewById(R.id.id_pos_info_lt_decimal_places)
        ltMan = view.findViewById(R.id.id_pos_info_lt_mc)
        ltSerial = view.findViewById(R.id.id_pos_info_lt_serial)
        ltModel = view.findViewById(R.id.id_pos_info_lt_model)
        ltVersion = view.findViewById(R.id.id_pos_info_lt_version)
    }

    private suspend fun onClickInit() {
        runProg(
            "POS${stringById(R.string.t1733460848_430)}",
            stringById(R.string.t1733460848_431)
        ) {
            Pos.reset()

            val setup = PosSetupInfo.get()
            ltLevel.set(setup.level)
            ltCountry.set(country(setup.currencyCode))
            ltScaling.set(setup.scaling)
            ltDecimal.set(setup.decimalPlaces)
            val modelInfo = PosModelInfo.get()
            ltMan.set(modelInfo.manufacturerCode)
            ltSerial.set(modelInfo.serialNumber)
            ltModel.set(modelInfo.model)
            ltVersion.set(modelInfo.softVersion)
        }
    }
}
